﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BusinessManagementStudio.Models.BusinessObject.Entities;
using MongoDB.Driver.Builders;

namespace BusinessManagementStudio.Models.BusinessObject.Services
{
    public class UserService : EntityService<User>
    {
        public IEnumerable<User> GetUserDetails(int limit, int skip)
        {
            var gamesCursor = this.MongoConnectionHandler.MongoCollection.FindAllAs<User>()
                .SetSortOrder(SortBy<User>.Descending(g => g.CreateDate))
                .SetLimit(limit)
                .SetSkip(skip)
                .SetFields(Fields<User>.Include(g => g.Id, g => g.UserName, g => g.UserType, g => g.Password, g => g.CreateDate));
            return gamesCursor;
        }

        public bool IsExistingUserAcct(UserRegistration user)
        {
            bool isFound = false;

            var UserAcct = from u in this.MongoConnectionHandler.MongoCollection.FindAllAs<User>()
                           where u.UserName == user.UserName && u.UserType == user.UserType
                           select u;

            if (UserAcct.Count() > 0)
                isFound = true;

            return isFound;
        }

        public bool IsCorrectUser(String UserName, String Password)
        {
            bool isCorrect = false;

            var UserAcct = from u in this.MongoConnectionHandler.MongoCollection.FindAllAs<User>()
                           where u.UserName == UserName && u.Password == Password
                           select u;

            if (UserAcct.Count() > 0)
                isCorrect = true;

            return isCorrect;
        }

        public User GetUserDetail(String UserName, String Password)
        {
            User UserAcct = (from u in this.MongoConnectionHandler.MongoCollection.FindAllAs<User>()
                             where u.UserName == UserName && u.Password == Password
                             select u).FirstOrDefault();

            return UserAcct;
        }

        public override void Update(User entity)
        {
            //// Not necessary for the example
        }
    }
}
